unit languagehelper;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, LCLTranslator;

function IsLanguageRtl(Code: String): Boolean;
procedure SetGlobalLanguage(Code: String);

var
  CurrentAppLanguage: String;
  CurrentAppIsRtl: Boolean;

implementation

uses
  mainformunit, databaseformunit, database, setmaximumformunit,  aboutformunit,
  imageselection, traitselector, expcurveformunit, parametercurvesformunit,
  parametercurvegeneratorformunit, editlearningformunit, iconselection,
  effectselector, dropitemselection, actionpatternselection,
  namedindexselection, doublebackgroundselection, battleventconditionsselection,
  newecformunit, eventeditorunit, windowskincolourformunit, globals;

function IsLanguageRtl(Code: String): Boolean;
begin
  //IsLanguageRtl := Application.IsRTLLang(Code)
  IsLanguageRtl := Code = 'uk'
end;

procedure SetGlobalLanguage(Code: String);
var
  IsRtl: Boolean;

  procedure ProcessForm(F: TForm);
  begin
    if IsRtl then begin
      F.BiDiMode := bdRightToLeft;
      if not F.IsFlipped then
        F.FlipChildren(True);
    end else begin
      F.BiDiMode := bdLeftToRight;
      if F.IsFlipped then
        F.FlipChildren(True);
    end;
  end;

begin
  IsRtl := IsLanguageRtl(Code);
  EditorLanguage := Code;

  SetDefaultLang(Code);
  ProcessForm(MainForm);
  MainForm.UpdateLanguage(Code);

  ProcessForm(DatabaseForm);
  DatabaseForm.UpdateLanguage(Code);

  ProcessForm(SetMaximumForm);
  ProcessForm(ImageSelectionForm);
  ProcessForm(TraitSelectorForm);
  ProcessForm(EffectSelectorForm);
  ProcessForm(AboutForm);
  ProcessForm(ExpCurveForm);
  ProcessForm(ParameterCurvesForm);
  ProcessForm(ParameterCurveGeneratorForm);
  ProcessForm(EditLearningForm);
  ProcessForm(IconSelectionForm);
  ProcessForm(DropItemSelectionForm);
  ProcessForm(ActionPatternSelectionForm);
  ProcessForm(NamedIndexSelectionForm);
  ProcessForm(DoubleBackgroundSelectionForm);
  ProcessForm(BattleEventConditionsSelectionForm);
  ProcessForm(NewECForm);
  ProcessForm(EventEditorForm);
  ProcessFOrm(WindowSkinColourForm);
end;

initialization
  //TODO: load languages from options
  CurrentAppLanguage := 'en';
  CurrentAppIsRtl := False;

end.

